
package com.app.loctracker;

import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        LocationListener mlocListener = new MyLocationListener(getApplicationContext());
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
        String content = "<data><lan>GPS</lan><lat>GPS</lat></data>";
        FileOutputStream fos;
        try {
            fos = getApplicationContext().openFileOutput("blocatordata.xml", Context.MODE_PRIVATE);
            for (int i = 0; i < content.length(); ++i)
                fos.write((byte)content.charAt(i));

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

class MyLocationListener extends Main implements LocationListener {
    Context context;

    List<Address> myList = null;

    public MyLocationListener(Context con) {
        // TODO Auto-generated constructor stub'
        context = con;
    }

    @Override
    public void onLocationChanged(Location loc) {
        // TODO Auto-generated method stub

        String Text = "My current location is: " + "Latitude = " + loc.getLatitude()
                + "Longitude = " + loc.getLongitude();
        Toast.makeText(context, Text, Toast.LENGTH_LONG).show();
        String content = "<data><lat>" + loc.getLatitude() + "</lat><lan>" + loc.getLongitude()
                + "</lan></data>";
       /* StringBuilder sb = new StringBuilder();
        if (loc != null) {
            try {
                Geocoder geocoder = new Geocoder(context, Locale.getDefault());
                myList = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 10);

                if (myList.size() > 0) {
                    Address address = myList.get(0);

                    for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
                        sb.append(address.getAddressLine(i)).append("\n");
                    sb.append(address.getLocality()).append("\n");
                    sb.append(address.getPostalCode()).append("\n");
                    sb.append(address.getCountryName());

                }

            }

            catch (IOException e) {
                e.printStackTrace();
            }

        }
        Toast.makeText(context, "Here:" + sb.toString(), Toast.LENGTH_SHORT).show();*/
        FileOutputStream fos;
        try {
            fos = context.openFileOutput("blocatordata.xml", Context.MODE_PRIVATE);
            for (int i = 0; i < content.length(); ++i)
                fos.write((byte)content.charAt(i));

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

}
